package com.calc.code.calc;

import android.widget.ArrayAdapter;



import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.AndroidCharacter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;


import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.textView)
    TextView textView;
    @Bind(R.id.homemoney)
    EditText homemoney;
    @Bind(R.id.year)
    EditText year;
    @Bind(R.id.rb1)
    RadioButton rb1;
    @Bind(R.id.rb2)
    RadioButton rb2;
    @Bind(R.id.rg)
    RadioGroup rg;
    @Bind(R.id.spinner)
    Spinner spinner;
    @Bind(R.id.textView2)
    TextView textView2;
    @Bind(R.id.activity_main)
    LinearLayout activityMain;
    @Bind(R.id.button)
    Button button;
    @Bind(R.id.listview)
    ListView listview;
    private boolean isBinuess = true;
    private boolean isCapital = true;

    public static double lili = 0.1f;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
                R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
        spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                Toast.makeText(MainActivity.this, "position" + position, Toast.LENGTH_SHORT).show();
                if (position == 0) {
                    isCapital = true;
                } else {
                    isCapital = false;
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rb1) {
                    isBinuess = true;
                } else {
                    isBinuess = false;
                }
            }
        });
    }

    @OnClick(R.id.button)
    public void onClick() {
        double homeMoney =Double.parseDouble(homemoney.getText().toString()) ;
        String year = this.year.getText().toString();
        int yearInt = Integer.parseInt(year);
//        每月还款金额 = （贷款本金 / 还款月数）+（本金 — 已归还本金累计额）×每月利率
        // homeMoney * 1.0f  / yearInt*12+

        //每年要还的钱数=要接的钱



        if(!isCapital){

            //denger binjin 计算公式
          //  [贷款本金×月利率×（1+月利率）^还款月数]÷[（1+月利率）^还款月数－1]
                double lilv = Math.pow((lili + 1), yearInt * 12);
            double money = (homeMoney*lilv*lili)/(lilv-1);
           // double money = Math.pow(homeMoney *(lili)* (lili + 1), yearInt * 12) / (Math.pow((1 + lili), yearInt * 12) - 1);
            //保留两位小数
             money = ((int) (money * 100)) / 100.0;
            ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new String[]{"每个月应还款:" + money});
            listview.setAdapter(stringArrayAdapter);

        }else{

/*
            每季还款额=贷款本金÷贷款期季数+（本金-已归还本金累计额）×季利率
            如：以贷款20万元，贷款期为10年，为例：
            每季等额归还本金：200000÷（10×4）=5000元
            第一个季度利息：200000×（5.58%÷4）=2790元
            则第一个季度还款额为5000+2790=7790元；
            第二个季度利息：（200000-5000×1）×（5.58%÷4）=2720元
            则第二个季度还款额为5000+2720=7720元*/
            List<String> result = new ArrayList<>(yearInt*12);

            /*-----------------------------------*/
            float haveReturnMoney = 0f;
            for (int i = 1; i <=yearInt*12; i++) {
                //计算每个月应还款
                double everyMonthMoney = (homeMoney / (yearInt * 12) )+ (homeMoney - haveReturnMoney) * lili;
                haveReturnMoney+=everyMonthMoney;
                //System.out.println(" "+everyMonthMoney );
                String s  = "第"+ i+"个月要还的钱："+everyMonthMoney;
                result.add(s);
                Toast.makeText(this," jj  "+lili,Toast.LENGTH_SHORT).show();

               /* if((homeMoney-haveReturnMoney)<0.00000005){
                    break;
                }*/


            }
            /*-----------------------------------*/
            Toast.makeText(this,"ddd"+result.size(),Toast.LENGTH_SHORT);
            ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, result);
            listview.setAdapter(stringArrayAdapter);


        }
    }


}
